package com.hhhn.wk.utils;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.widget.Toast;

import com.hhhn.wk.base.BaseActivity;


/**
 * Created by Administrator on 2017/4/5.
 * wk
 * 定义广播  判断网络情况
 * 1.逻辑
 * 2.动态开启
 * br=new NetBroadCastReciver(this);
 * IntentFilter itFilter = new IntentFilter();
 * itFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
 * registerReceiver(br, itFilter);
 * 3.   结束    unregisterReceiver(br);
 */

public class NetBroadCastReciver extends BroadcastReceiver {
    ConnectivityManager manager;
    BaseActivity activity;
//    int sum=0;
private AlertDialog alert = null;
    private AlertDialog.Builder builder = null;
    public NetBroadCastReciver(BaseActivity activity) {
        this.activity = activity;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean flag = false;
        //得到网络连接信息
        manager = (ConnectivityManager)activity.getSystemService(Context.CONNECTIVITY_SERVICE);

        //去进行判断网络是否连接
        if (manager.getActiveNetworkInfo() != null) {
            flag = manager.getActiveNetworkInfo().isAvailable();
        }
        if (!flag) {
            setNetwork();
        } else {
//            Toast.makeText(activity, "网络已连接", Toast.LENGTH_SHORT).show();
            if (builder!=null&&alert!=null){
                alert.dismiss();
            }
//            if (sum==0){
////                isNetworkAvailable();
//            }

        }

    }
    /**
     * 网络未连接时，调用设置方法
     */
    private void setNetwork(){
//        sum=0;
        Toast.makeText(activity, "网络未连接", Toast.LENGTH_SHORT).show();
        builder = new AlertDialog.Builder(activity);
//        builder.setIcon(R.drawable.ic_launcher);

        alert = builder.setTitle("网络提示信息")
                .setMessage("网络不可用，如果继续，请先设置网络！")
//                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
////                        Toast.makeText(mContext, "你点击了取消按钮~", Toast.LENGTH_SHORT).show();
//                    }
//                })
                .setPositiveButton("设置", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = null;
                        /**
                         * 判断手机系统的版本！如果API大于10 就是3.0+
                         * 因为3.0以上的版本的设置和3.0以下的设置不一样，调用的方法不同
                         */
                        if (android.os.Build.VERSION.SDK_INT > 10) {
                            intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
                        } else {
                            intent = new Intent();
                            ComponentName component = new ComponentName(
                                    "com.android.settings",
                                    "com.android.settings.WirelessSettings");
                            intent.setComponent(component);
                            intent.setAction("android.intent.action.VIEW");
                        }
                        activity.startActivity(intent);
                    }
                }).create();             //创建AlertDialog对象


        builder.setCancelable(false);
        alert.show();                    //显示对话框


    }

//    /**
//     * 网络已经连接，然后去判断是wifi连接还是GPRS连接
//     * 设置一些自己的逻辑调用
//     */
//    private void isNetworkAvailable(){
//        NetworkInfo.State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
//        NetworkInfo.State wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
//        if(gprs == NetworkInfo.State.CONNECTED || gprs == NetworkInfo.State.CONNECTING){
//
//            Toast.makeText(activity, "GPRS连接", Toast.LENGTH_SHORT).show();
//          builder = new AlertDialog.Builder(activity);
////        builder.setIcon(R.drawable.ic_launcher);
//            builder.setTitle("网络提示信息");
//            builder.setMessage("当前网络为数据流量模式");
//            builder.setPositiveButton("我是土豪", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    dialog.dismiss();
//                }
//            });
//
//            builder.setNegativeButton("设置", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    Intent intent = null;
//                    /**
//                     * 判断手机系统的版本！如果API大于10 就是3.0+
//                     * 因为3.0以上的版本的设置和3.0以下的设置不一样，调用的方法不同
//                     */
//                    if (android.os.Build.VERSION.SDK_INT > 10) {
//                        intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
//                    } else {
//                        intent = new Intent();
//                        ComponentName component = new ComponentName(
//                                "com.android.settings",
//                                "com.android.settings.WirelessSettings");
//                        intent.setComponent(component);
//                        intent.setAction("android.intent.action.VIEW");
//                    }
//                    activity.startActivity(intent);
//                }
//            });
//            builder.create();
//            builder.show();
//        }
//        //判断为wifi状态下才加载广告，如果是GPRS手机网络则不加载！
//        if(wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING){
//
//            Toast.makeText(activity, "wifi链接", Toast.LENGTH_SHORT).show();
//            loadAdmob();
//        }
//        sum=1;
//    }

}
